package com.waimai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.PageList;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.waimai.common.BaseContext;
import com.waimai.common.R;
import com.waimai.domain.Employee;
import com.waimai.service.EmployeeService;
import com.waimai.service.imlp.EmployeeServiceImlp;
import org.apache.tomcat.util.buf.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.invoke.LambdaConversionException;
import java.time.LocalDateTime;
import java.util.List;


@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    EmployeeService ES;


    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
//        MD5加密
        String password = employee.getPassword();
       password = DigestUtils.md5DigestAsHex(password.getBytes());
       //查询数据库（Mybatis-plus）
        LambdaQueryWrapper<Employee> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = ES.getOne(objectLambdaQueryWrapper);
        //查到判断
        if(emp == null){
            return R.error("用户名不存在");
        }
        if(!emp.getPassword().equals(password)){
            return R.error("密码错误");
        }
        if(emp.getStatus()==0){
            return R.error("账号已被禁用");
        }
        //登录成功
        request.getSession().setAttribute("employee",emp.getId());


        return R.success(emp);


    }
    @PostMapping("/logout")
    public  R<String> logout(HttpServletRequest request){

       request.getSession().removeAttribute("employee");


        return  R.success("退出成功");
    }
    //增加员工
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        //初始密码123456  并MD5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        Long userid = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(userid);
//        employee.setUpdateUser(userid);
        ES.save(employee);

        return R.success("增加成功");
    }
    //员工分页查询
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize,String name){
        //分页器
        Page page1 = new Page(page,pageSize);
        LambdaQueryWrapper<Employee>  queryWrapper= new LambdaQueryWrapper();
        queryWrapper.like(name !=null,Employee::getName,name);
        //排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //执行查询
        ES.page(page1,queryWrapper);
        return R.success(page1);

    }
    @PutMapping
    public R<String> updata(HttpServletRequest request,@RequestBody Employee employee){
//         employee.setUpdateTime(LocalDateTime.now());
//         Long r =(Long)request.getSession().getAttribute("employee");
//         employee.setUpdateUser(r);
         ES.updateById(employee);
        return R.success("1");
    }
    //员工界面编辑回显
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee employee = ES.getById(id);
        if (employee.getName()!= null){
            return R.success(employee);
        }
        return R.error("编辑失败");
    }
}
